﻿using System.Web;
using System.Web.UI;

namespace Common
{
    /// <summary>
    /// javascript公共类 lzy 2011.6.14
    /// </summary>
    public static class JscriptHelper
    {
        private static int i = 0;
        /// <summary>
        /// 执行输入的js脚本
        /// </summary>
        /// <param name="script">需要执行的js</param>
        public static void MyExecute(string script)
        {
            var page = HttpContext.Current.CurrentHandler as Page;
            if (page != null)
            {
                page.ClientScript.RegisterStartupScript(page.GetType(), "script" + i++, script, true);
            }
        }
        /// <summary>
        /// 页面后执行
        /// </summary>
        /// <param name="js">js</param>
        public static void ExecutePreRender(string js)
        {
            var page = HttpContext.Current.CurrentHandler as Page;
            if (page != null)
            {
                page.ClientScript.RegisterStartupScript(page.GetType(), "script" + i++, js, true);
            }
        }
        #region 基本javascript方法
        /// <summary>
        /// 弹出提示框
        /// </summary>
        /// <param name="msg">提示框弹出的信息</param>
        public static void MyAlert(string msg)
        {
            MyExecute("alert('" + msg + "');");
        }

        /// <summary>
        /// 刷新当前页面
        /// </summary>
        public static void MyReload()
        {
            MyExecute("location=location;");
        }


        /// <summary>
        /// 重定向当前页面
        /// </summary>
        /// <param name="url">需要定向的页面</param>
        public static void MyRediect(string url)
        {
            MyExecute("location='" + url + "';");
        }

        /// <summary>
        /// 弹出提示框，并刷新当页面
        /// </summary>
        /// <param name="msg"></param>
        public static void MyAlertAndReload(string msg)
        {
            MyExecute("alert('" + msg + "');location=location;");
        }
        /// <summary>
        /// 弹出提示框，并重定向到新页面
        /// </summary>
        /// <param name="msg">弹出提示框显示的内容</param>
        /// <param name="url">提示完毕后，重定向到的页面</param>
        public static void MyAlertAndRediect(string msg, string url)
        {
            MyExecute("alert('" + msg + "');location='" + url + "';");
        }

        /// <summary>
        /// 设置元素的HTML
        /// </summary>
        /// <param name="id"></param>
        /// <param name="content"></param>
        public static void InnerHTML(string id, string content)
        {
            string js = string.Format("document.getElementById(\"{0}\").innerHTML=\"{1}\";", id, content);
            ExecutePreRender(js);
        }
        /// <summary>
        /// 设置元素的Text
        /// </summary>
        /// <param name="id"></param>
        /// <param name="content"></param>
        public static void InnerText(string id, string content)
        {
            string js = string.Format("document.getElementById(\"{0}\").innerText=\"{1}\";", id, content);
            ExecutePreRender(js);
        }
        /// <summary>
        /// 设置元素的Value
        /// </summary>
        /// <param name="id"></param>
        /// <param name="content"></param>
        public static void Value(string id, string content)
        {
            string js = string.Format("document.getElementById(\"{0}\").value=\"{1}\";", id, content);
            ExecutePreRender(js);
        }
        /// <summary>
        /// 把一个元素给display了。
        /// </summary>
        /// <param name="id">元素的ID</param>
        /// <param name="b">true为显示。false为隐藏</param>
        public static void Display(string id, bool b)
        {
            string js = string.Format("document.getElementById(\"{0}\").style.display=\"{1}\";", id, b ? "" : "none");
            MyExecute(js);
        }
        #endregion

        #region DWZ框架 iframe中用 javascript 方法

        #region 提示
        /// <summary>
        /// Dwz弹出错误提示框 iframe框架
        /// </summary>
        /// <param name="msg">弹出错误提示框</param>
        public static void DwzIfmAlertError(string msg)
        {
            MyExecute("parent.alertMsg.error('" + msg + "');");
        }
        /// <summary>
        /// Dwz弹出信息提示框 iframe框架
        /// </summary>
        /// <param name="msg">弹出信息提示框</param>
        public static void DwzIfmAlertInfo(string msg)
        {
            MyExecute("parent.alertMsg.info('" + msg + "');");
        }
        /// <summary>
        /// Dwz弹出警告提示框 iframe框架
        /// </summary>
        /// <param name="msg">弹出警告提示框</param>
        public static void DwzIfmAlertWarn(string msg)
        {
            MyExecute("parent.alertMsg.warn('" + msg + "');");
        }
        /// <summary>
        /// Dwz弹出成功提示框 iframe框架
        /// </summary>
        /// <param name="msg">弹出成功提示框</param>
        public static void DwzIfmAlertCorrect(string msg)
        {
            MyExecute("parent.alertMsg.correct('" + msg + "');");
        }
        #endregion

        /// <summary>
        /// 刷新当前页面
        /// </summary>
        public static void DwzIfmReload()
        {
            MyExecute("location=location;");
        }


        /// <summary>
        /// 重定向当前页面
        /// </summary>
        /// <param name="url">需要定向的页面</param>
        public static void DwzIfmRediect(string url)
        {
            MyExecute("location='" + url + "';");
        }

        /// <summary>
        /// 弹出提示框，成功提示并刷新当页面
        /// </summary>
        /// <param name="msg"></param>
        public static void DwzIfmAlertCorrectAndReload(string msg)
        {
            MyExecute("parent.alertMsg.correct('" + msg + "');location=location;");
        }

        /// <summary>
        /// 弹出提示框，错误提示并刷新当页面
        /// </summary>
        /// <param name="msg"></param>
        public static void DwzIfmAlertErrorAndReload(string msg)
        {
            MyExecute("parent.alertMsg.error('" + msg + "');location=location;");
        }
        #region 新添加  主后台用 新打开页面中 嵌套iframe 页面用
        /// <summary>
        /// 弹出提示框，成功提示并刷新当页面  两次嵌套iframe用
        /// </summary>
        /// <param name="msg"></param>
        public static void DwzIfmParentAlertCorrectAndReload(string msg)
        {
            MyExecute("parent.parent.alertMsg.correct('" + msg + "');location=location;");
        }
        /// <summary>
        /// 弹出提示框，错误提示并刷新当页面  两次嵌套iframe用
        /// </summary>
        /// <param name="msg"></param>
        public static void DwzIfmParentAlertErrorAndReload(string msg)
        {
            MyExecute("parent.parent.alertMsg.error('" + msg + "');location=location;");
        }
        /// <summary>
        /// 弹出提示框，错误提示并重定向到新页面  两次嵌套iframe用
        /// </summary>
        /// <param name="msg"></param>
        public static void DwzIfmParentAlertCorrectAndRediect(string msg, string url)
        {
            MyExecute("parent.parent.alertMsg.correct('" + msg + "');location='" + url + "';");
        }
        #endregion

        #region 弹出提示框，成功提示并重定向到新页面，适用于frame框架(showdialog)。
        /// <summary>
        /// 弹出提示框，成功提示并重定向到新页面，适用于frame框架。  
        /// </summary>
        /// <param name="msg">弹出提示框显示的内容</param>
        /// <param name="url">提示完毕后，重定向到的页面</param>
        public static void DwzIfmAlertCorrectAndRediectAll(string msg, string url)
        {
            MyExecute("alert('" + msg + "');window.open('" + url + "','_top');");
        }
        #endregion

        /// <summary>
        /// 弹出提示框，成功提示并重定向到新页面
        /// </summary>
        /// <param name="msg">弹出提示框显示的内容</param>
        /// <param name="url">提示完毕后，重定向到的页面</param>
        public static void DwzIfmAlertCorrectAndRediect(string msg, string url)
        {
            MyExecute("parent.alertMsg.correct('" + msg + "');location='" + url + "';");
        }
        /// <summary>
        /// 弹出提示框，错误提示并重定向到新页面
        /// </summary>
        /// <param name="msg">弹出提示框显示的内容</param>
        /// <param name="url">提示完毕后，重定向到的页面</param>
        public static void DwzIfmAlertErrorAndRediect(string msg, string url)
        {
            MyExecute("parent.alertMsg.error('" + msg + "');location='" + url + "';");
        }
        #endregion
    }
}
